<?php
/**
 * User: Drery
 * Date: 2015/12/21
 * Time: 9:59
 */

namespace backend\controllers;

use Yii;
use backend\models\Permission;
use yii\data\ActiveDataProvider;
use yii\helpers\Url;
use yii\web\NotFoundHttpException;

class PermissionController extends InitController
{
    public function actionIndex()
    {
        $pid = Yii::$app->request->get('pid') ?: 0;
        $parentModel = $pid == 0 ? (object)['id' => $pid, 'description' => '顶级分类', 'level' => 0] : Permission::findOne(['id' => $pid, 'is_deleted' => 0]);
        $breadcrumb = $this->generateBreadcrumb($parentModel, $parentModel->level);
        if (!$parentModel) {
            throw new NotFoundHttpException;
        }
        $dataProvider = new ActiveDataProvider([
            'query' => Permission::find()->where(['pid' => $pid,'is_deleted'=>0])->orderBy(['arrangement' => SORT_ASC]),
            'sort' => false
        ]);
        return $this->render('index', [
            'dataProvider' => $dataProvider,
            'parentModel' => $parentModel,
            'breadcrumb' => $breadcrumb
        ]);
    }

    private function generateBreadcrumb($parentModel, $leastLevel, $links = [])
    {
        if ($parentModel->level == 0) {
            $parentModel->description = $this->permission->description;
        }
        $link = $leastLevel == $parentModel->level ? $parentModel->description : ['label' => $parentModel->description, 'url' => Url::toRoute(['/permission', 'pid' => $parentModel->id])];
        array_unshift($links, $link);
        if ($parentModel->level == 0) {
            return $links;
        }
        $grandModel = $parentModel->level == 1 ? (object)['id' => 0, 'level' => 0] : Permission::find()->where(['id' => $parentModel->pid, 'is_deleted' => 0])->one();
        return $this->generateBreadcrumb($grandModel, $leastLevel, $links);
    }

    public function actionAdd()
    {
        $model = new Permission();
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                return $this->redirect(['index','pid'=>Yii::$app->request->get('pid')]);
            }
        }
        $pid = Yii::$app->request->get('pid');
        $parentModel = $pid == 0 ? (object)['id' => $pid, 'description' => '顶级分类', 'level' => 0] : Permission::findOne(['id' => $pid, 'is_deleted' => 0]);
        $level = $parentModel->level + 1;
        return $this->render('add',[
            'parentModel' => $parentModel,
            'model' => $model,
            'pid' => $pid,
            'level' => $level
        ]);
    }

    public function actionUpdate()
    {
        $id = Yii::$app->request->get('id');
        $model = Permission::findOne($id);
        if (!$model) {
            throw new NotFoundHttpException;
        }
        if (Yii::$app->request->isPost && $model->load(Yii::$app->request->post())) {
            if ($model->save()) {
                return $this->redirect(['index','pid'=>$model->pid]);
            }
        }
        return $this->render('update',[
            'model' => $model
        ]);
    }

    public function actionDelete()
    {
        $id = Yii::$app->request->get('id');
        $model = Permission::findOne($id);
        if (!$model) {
            throw new NotFoundHttpException;
        }
        $model->is_deleted = 1;
        $model->save();
        return $this->redirect(['index','pid'=>$model->pid]);
    }
}